package com.uls.zsw.service.impl;

import java.util.List;
import com.uls.common.utils.DateUtils;
import com.uls.czj.domain.CzjStudents;
import com.uls.czj.mapper.CzjStudentsMapper;
import com.uls.zsw.domain.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.uls.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.uls.zsw.mapper.DormitoryInfoMapper;
import com.uls.zsw.domain.DormitoryInfo;
import com.uls.zsw.service.IDormitoryInfoService;

/**
 * 宿舍信息Service业务层处理
 * 
 * @author zsw
 * @date 2024-11-07
 */
@Service
public class DormitoryInfoServiceImpl implements IDormitoryInfoService 
{
    @Autowired
    private DormitoryInfoMapper dormitoryInfoMapper;

    @Autowired

    private CzjStudentsMapper czjStudentsMapper;

    /**
     * 查询宿舍信息
     * 
     * @param dormitoryId 宿舍信息主键
     * @return 宿舍信息
     */
    @Transactional
    @Override
    public DormitoryInfo selectDormitoryInfoByDormitoryId(Long dormitoryId)
    {
        DormitoryInfo dormitoryInfo=dormitoryInfoMapper.selectDormitoryInfoByDormitoryId(dormitoryId);
        String DN=dormitoryInfo.getDormitoryNumber();
        List<CzjStudents> czjStudentsList=czjStudentsMapper.findstudentBycorrentDormitory(DN);
        List<StudentVo> studentVoList=new ArrayList<>();
        for(CzjStudents czjStudents1:czjStudentsList){
            StudentVo studentVo1=new StudentVo();
            studentVo1.setName(czjStudents1.getName());
            studentVo1.setStudentNumber(czjStudents1.getStudentNumber());
            studentVo1.setStudentId(czjStudents1.getStudentId());
            studentVo1.setPhoneNumber(czjStudents1.getPhoneNumber());
            studentVoList.add(studentVo1);
        }
        dormitoryInfo.setCzjStudentsList(studentVoList);
        return dormitoryInfo;
    }

    /**
     * 查询宿舍信息列表
     * 
     * @param dormitoryInfo 宿舍信息
     * @return 宿舍信息
     */
    @Transactional
    @Override
    public List<DormitoryInfo> selectDormitoryInfoList(DormitoryInfo dormitoryInfo)
    {
        return dormitoryInfoMapper.selectDormitoryInfoList(dormitoryInfo);
    }

    /**
     * 新增宿舍信息
     * 
     * @param dormitoryInfo 宿舍信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDormitoryInfo(DormitoryInfo dormitoryInfo)
    {
        dormitoryInfo.setCreateTime(DateUtils.getNowDate());
        int rows = dormitoryInfoMapper.insertDormitoryInfo(dormitoryInfo);
        String dormitorynumber=dormitoryInfo.getDormitoryNumber();
        List<StudentVo> studentVoList=dormitoryInfo.getCzjStudentsList();
        for(StudentVo studentVo1:studentVoList){
            czjStudentsMapper.addcorrentDormitory(dormitorynumber,studentVo1.getStudentNumber());
        }
        System.out.println(czjStudentsMapper.findstudentBycorrentDormitory(dormitorynumber));
        return rows;
    }

    /**
     * 修改宿舍信息
     * 
     * @param dormitoryInfo 宿舍信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDormitoryInfo(DormitoryInfo dormitoryInfo)
    {
        System.out.println(dormitoryInfo.getCzjStudentsList());
        List<StudentVo> studentVoList=dormitoryInfo.getCzjStudentsList();
        String dormitorynumber=dormitoryInfo.getDormitoryNumber();
        for(StudentVo studentVo1:studentVoList){
            czjStudentsMapper.addcorrentDormitory(dormitorynumber,studentVo1.getStudentNumber());
        }
        return dormitoryInfoMapper.updateDormitoryInfo(dormitoryInfo);
    }

    /**
     * 批量删除宿舍信息
     * 
     * @param dormitoryIds 需要删除的宿舍信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDormitoryInfoByDormitoryIds(Long[] dormitoryIds)
    {
        for(Long Did:dormitoryIds){
            DormitoryInfo dormitoryInfo=dormitoryInfoMapper.selectDormitoryInfoByDormitoryId(Did);
            List<CzjStudents> czjStudentsList=czjStudentsMapper.findstudentBycorrentDormitory(dormitoryInfo.getDormitoryNumber());
            for(CzjStudents czjStudents:czjStudentsList){
                dormitoryInfoMapper.deleteCzjStudentsByStudentId(czjStudents.getStudentId());
            }
        }

        return dormitoryInfoMapper.deleteDormitoryInfoByDormitoryIds(dormitoryIds);
    }

    /**
     * 删除宿舍信息信息
     * 
     * @param dormitoryId 宿舍信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDormitoryInfoByDormitoryId(Long dormitoryId)
    {
        DormitoryInfo dormitoryInfo=dormitoryInfoMapper.selectDormitoryInfoByDormitoryId(dormitoryId);
        List<CzjStudents> czjStudentsList=czjStudentsMapper.findstudentBycorrentDormitory(dormitoryInfo.getDormitoryNumber());
        for(CzjStudents czjStudents:czjStudentsList){
            dormitoryInfoMapper.deleteCzjStudentsByStudentId(czjStudents.getStudentId());
        }
        return dormitoryInfoMapper.deleteDormitoryInfoByDormitoryId(dormitoryId);
    }


    /**
     * 新增学生信息信息
     * 
     * @param dormitoryInfo 宿舍信息对象
     */
    public void insertCzjStudents(DormitoryInfo dormitoryInfo){}

    /**
     * 根据studentNumber获取学生信息
     * @param studentNumber
     * @return
     */
    @Override
    public StudentVo findStudentByStudentNumber(Long studentNumber) {
        return dormitoryInfoMapper.findStudentByStudentNumber(studentNumber);
    }
    @Override
    public List<String> selectAllDormitoryNumber() {
        return dormitoryInfoMapper.selectAllDormitoryNumber();
    }

    @Override
    public void delStudentinDormitory(Long[] studentIds) {
        for (Long studentid : studentIds) {
            dormitoryInfoMapper.delStudentinDormitory(studentid);
        }
    }
}
